﻿using CUREBusinessLogicLayer.Harmful;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CURE.Handler
{
    /// <summary>
    /// QAHandler 的摘要说明
    /// </summary>
    public class QAHandler : IHttpHandler
    {

        private string approvalvalue;

        public string Approvalvalue
        {
            get { return approvalvalue; }
            set { approvalvalue = value; }
        }
        private string approvalreason;

        public string Approvalreason
        {
            get { return approvalreason; }
            set { approvalreason = value; }
        }

        private LedgerBll ledgerbll = new LedgerBll();

        public void ProcessRequest(HttpContext context)
        {
            string page = context.Request.Form["page"];
            if (page == "harmful")
            {
                Approvalreason = context.Request.Form["appreason"];
                Approvalvalue = context.Request.Form["approval"];
                string state = "0";
                if (Approvalvalue == "0")
                {
                    state = "0";
                }
                else
                {
                    state = "1";
                }

                if (SkillApproval(context.Request.Form["Ledgerid"],context.Request.Form["Fid"],Approvalreason,state))
                {
                    context.Response.Write(success("Harmful/ExpResult.aspx"));
                }
                else
                {
                    context.Response.Write(failure());
                }
            }
            else
            {
                Approvalreason = context.Request.Form["appreason"];
                Approvalvalue = context.Request.Form["approval"];
                string state = "0";
                if (Approvalvalue == "0")
                {
                    state = "2";
                }
                else
                {
                    state = "-1";
                }
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("reason", Approvalreason);
                dic.Add("state", state);
                dic.Add("id", context.Request.Form["id"]);
                dic.Add("ledgerid", context.Request.Form["ledgerid"]);

                if (QAApproval(dic))
                {
                    context.Response.Write(success("Experiment/ExpApproval.aspx"));
                }
                else
                {
                    context.Response.Write(failure());
                }
            }

        }

        /// <summary>
        /// QA审核实验结果
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        private bool QAApproval(IDictionary<string, object> dic)
        {
            return ledgerbll.QAApproval(dic);
        }

        /// <summary>
        /// 更新审核结果(技术领导审核实验完毕信息)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="fid"></param>
        /// <param name="reason"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        private bool SkillApproval(string id, string fid, string reason, string state)
        {
            return ledgerbll.ExpResultSkillApproval(id, fid, reason, state);
        }

        #region 提示信息
        /// <summary>
        /// 操作失败(无参数)
        /// </summary>
        /// <returns></returns>
        public string failure()
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
        }
        /// <summary>
        /// 操作失败(有参数)
        /// </summary>
        /// <param name="str">错误提示</param>
        /// <returns></returns>
        public string failure(string str)
        {
            return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
        }
        /// <summary>
        /// 操作成功(无参数)
        /// </summary>
        /// <returns></returns>
        public string success()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"navTabId\":\"editLedger\", \"callbackType\":\"closeCurrent\"}";
        }

        public string success1()
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
        }

        /// <summary>
        /// 操作成功(有参数)
        /// </summary>
        /// <param name="str">信息</param>
        /// <param name="navTabId">页面指向</param>
        /// <returns></returns>
        public string success(string str, string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
        }
        public string successfoward(string str, string url)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"forward\",\"forwardUrl\":\"" + url + "\"}";
        }
        public string success(string navTabId)
        {
            return "{\"statusCode\":\"200\", \"message\":\"操作成功!\",\"callbackType\":\"closeCurrent\",\"navTabId\":\"" + navTabId + "\"}";
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}